package com.example.hasee.zhcd.Activities.ChuanWu;

import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Adapters.ArrivalsEntrustAdapter;
import com.example.hasee.zhcd.Adapters.LeavePortEntrustAdapter;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by hasee on 2017/10/13.
 */

public class ArrivalEntrustActivity extends BaseActivity {
    private TextView title;
    private ImageView back;
    private ImageView home;
    private ExpandableListView lp_entrust_list;
    private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private ArrayList<ArrayList<String>> son_list = new ArrayList<ArrayList<String>>();
    private ArrivalsEntrustAdapter adapter;
    private String strShipCode;
    private String strShipYearNum;

    @Override
    public int contentViewId() {
        return R.layout.activity_arrival_entrust;
    }

    @Override
    public void set() {
        strShipCode = getIntent().getExtras().getString("fshipcode");
        strShipYearNum = getIntent().getExtras().getString("fshipyearnumvcr");
        title.setText("委办事项");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
        getLeavePortEntrust();
    }

    @Override
    public void init() {
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        lp_entrust_list = (ExpandableListView) findViewById(R.id.lp_entrust_list);
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "Fail":
                ProgressDialogUtils.closeProgressDialog();
                Toast.makeText(ArrivalEntrustActivity.this, "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "YWCX_lgcb_WbsxList":
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        list.clear();
                        son_list.clear();
                        for (int i = 0; i < data.length(); i++) {
                            HashMap<String, String> lemap = new HashMap<String, String>();
                            lemap.put("fzt", data.getJSONObject(i).getString("fzt"));
                            lemap.put("ftznr", data.getJSONObject(i).getString("ftznr"));
                            lemap.put("fwtsx", data.getJSONObject(i).getString("fwtsx"));
                            lemap.put("fqrr", data.getJSONObject(i).getString("fqrr"));
                            lemap.put("fqrsj", data.getJSONObject(i).getString("fqrsj"));
                            lemap.put("ftzr", data.getJSONObject(i).getString("ftzr"));
                            lemap.put("ftzsj", data.getJSONObject(i).getString("ftzsj"));
                            lemap.put("fdlr", data.getJSONObject(i).getString("fdlr"));
                            lemap.put("fdlsj", data.getJSONObject(i).getString("fdlsj"));
                            ArrayList<String> strings = new ArrayList<>();
                            strings.add(data.getJSONObject(i).getString("ftznr"));
                            list.add(lemap);
                            son_list.add(strings);
                        }
                        adapter = new ArrivalsEntrustAdapter(ArrivalEntrustActivity.this, list, son_list);
                        lp_entrust_list.setAdapter(adapter);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void getLeavePortEntrust() {
        ProgressDialogUtils.createProgressDialog(ArrivalEntrustActivity.this, "正在获取数据...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strShipCode", strShipCode);
        map.put("strShipYearNum", strShipYearNum);
        httpUtils.get("YWCX_lgcb_WbsxList", map, "YWCX_lgcb_WbsxList");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}